// pages/personal/personal.js
var common = require("../../common/common.js").common;

var shoppingCar = require("../../common/shoppingCar.js");

var colorParse = require("../../common/color.js");

var app = getApp();

var baseInfo = require("../../siteinfo.js");

var util = require("../../utils/util.js");

var {$author: $author} = require("../../components/base/authorization.js");

Page({
    /**
   * 页面的初始数据
   */
    data: {
        alert_title: "提示",
        YouAre: "您自己是",
        Cantswitch: "不能切换",
        Theapplicationis: "申请正在审核中，请耐心等待！",
        Theaccounthasbeenfrozen: "账户已被冻结，如需解冻请联系平台管理员！",
        Haveappliedforfreeshop: "已申请免费开店",
        Applicationsubmitted: "已提交申请",
        Brandtenantsareunder: "品牌入驻商审核中,请耐心等待...",
        Brandtenanthasexpired: "品牌入驻商已过期",
        Bindthephonesuccessfully: "绑定手机成功",
        Failedtobindphone: "绑定手机失败",
        user_info_notice: 1,
        // 默认弹出头像昵称补充
        num_isShow: true,
        page_sign: "personal",
        cart_count: 0,
        info: [],
        amount0: "",
        amount1: "",
        amount7: "",
        amount30: "",
        alert: 0,
        color_header: "",
        color_main: "",
        color_minor: "",
        group: [],
        clerk: [],
        leader_change: 2,
        phone: "",
        l_supply: 0,
        showCode: 0,
        code_img: "",
        rp_name: "",
        isIPX: "",
        orderNum: [],
        version: "",
        copyTitle: "",
        rulesIsHide: false,
        merStatus: 0,
        menu1: "供应商",
        menu2: "本地商家",
        menu3: "区域团长",
        vipStatus: "",
        serviceIconList: [],
        tabBar: [],
        tabBar1: [ {
            iconName: "icon-shouye1",
            tip: "首页",
            current: 0,
            num: 0,
            url: "/pages/index/index",
            openType: "switchTab"
        }, {
            iconName: "icon-fenlei1",
            tip: "分类",
            current: 0,
            num: 0,
            url: "/pages/classfit/classfit",
            openType: "switchTab"
        }, {
            iconName: "icon-gouwuche2",
            tip: "购物车",
            current: 0,
            num: 0,
            url: "/pages/shopcart/shopcart"
        }, {
            iconName: "icon-dingdan2",
            tip: "订单",
            current: 0,
            num: 0,
            url: "/pages/order/order",
            openType: "switchTab"
        }, {
            iconName: "icon-wode1",
            tip: "我的",
            current: 1,
            num: 0,
            url: "/pages/personal/personal",
            openType: "switchTab"
        } ],
        subscribeMessage: true,
        tmplIds: [],
        //模板消息tmplIds
        userAuditStatus: 0,
        t_cart_num: 0
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
    },
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        var that = this;
        if (wx.getStorageSync("changeUidOrder")) {
            //代表，切换了城市
            wx.removeStorage({
                key: "changeUidMy"
            });
        }
        wx.hideTabBar();
        var isIPX = app.globalData.isIPX;
        that.setData({
            isIPX: isIPX
        });
        that.setData({
            baseInfo: baseInfo.version
        });
        //登录刷新获取code
                that.login();
        //首次直接进入，拿user_id
                if (app.globalData.user) {
            //如果直接拿到openid
            that.setData({
                uid: app.globalData.user.uid
            });
            wx.setStorageSync("user", app.globalData.user);
            that.requestStart();
        } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                that.setData({
                    uid: res.uid
                });
                wx.setStorageSync("user", res);
                that.requestStart();
            });
        }
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
    },
    getVipInfo: function() {
        let that = this;
        common.request({
            endpoint: "vipcard/vip-verify",
            method: "POST",
            data: "",
            success: res => {
                that.setData({
                    vipStatus: res.extra.vip_card
                });
            }
        });
    },
    toOrder: function(e) {
        wx.setStorageSync("order_choose_nav", e.currentTarget.dataset.id);
        wx.switchTab({
            url: e.currentTarget.dataset.url
        });
    },
    requestStart: function() {
        this.clerk();
        //核销员
                common.getBottomIcon(5).then(() => {
            common.getCart();
        });
        this.getBase();
        //基础信息
                this.getVipInfo();
        this.getData();
        this.getuserinfo();
        //判断用户信息是否完美
                this.getOrdernum();
        this.getVersion();
        this.balanceIsOpen();
        //判断余额功能是否开通
                this.getMerStatus();
        this.getServiceIcon();
        this.getMyLeaderInfo();
        //我的团长信息
                this.getTotalData();
        //
                var newShoppingCart = shoppingCar.getInfo();
        this.setData({
            cart_count: newShoppingCart.cart_count
        });
        wx.stopPullDownRefresh();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 关闭弹框
   */
    back: function() {
        this.setData({
            close: 0
        });
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {
        this.setData({
            num_isShow: false
        });
    },
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {
        this.setData({
            num_isShow: false
        });
    },
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        this.requestStart();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    //品牌入驻商状态
    getLodgerStatus: function() {
        var that = this;
        common.request({
            endpoint: "Lodger/lodger/apply-status",
            method: "post",
            data: {},
            success(res) {
                that.setData({
                    lodgerStatus: res.extra
                });
            }
        });
    },
    //余额功能是否开通
    balanceIsOpen(totalPrice) {
        var that = this;
        common.request({
            endpoint: "Balance/balance/balance_switch",
            method: "post",
            data: {},
            success(res) {
                that.setData({
                    balanceSwitch: res.extra
                });
            }
        });
        common.request({
            endpoint: "Balance/balance/balance-set-read",
            method: "post",
            data: {},
            success(res) {
                that.setData({
                    balanceCashSwitch: res.extra.switch
                });
            }
        });
    },
    //请求服务图标
    getServiceIcon: function() {
        var that = this;
        common.request({
            endpoint: "set/service_icon",
            method: "post",
            data: {},
            success(res) {
                that.setData({
                    serviceIconList: res.extra
                });
            }
        });
    },
    /**
   * 用户点击右上角分享
   */
    //全局基础配置信息
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            data: {},
            success: res => {
                var copyText = [];
                if (res.extra.copyright_msg) {
                    var copy = res.extra.copyright_msg;
                    if (copy.indexOf(",") != -1) {
                        copyText = copy.split(",");
                    } else {
                        copyText[0] = copy;
                    }
                }
                var rgba = colorParse.hexToRgb(res.extra.color_main, .8);
                that.setData({
                    color_main: res.extra.color_main,
                    color_main2: rgba,
                    color_minor: res.extra.color_minor,
                    apptitle: res.extra.title,
                    style: res.extra.style,
                    leader_change: res.extra.l_change,
                    phone: res.extra.phone,
                    copyImg: res.extra.copyright_img,
                    copyText: copyText,
                    l_supply: res.extra.l_supply,
                    rp_name: res.extra.rp_name,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    leader_address_on: res.extra.leader_address_on,
                    vip_card_on: res.extra.vip_card_on,
                    plant_show: res.extra.plant_show,
                    header_bg: res.extra.header_bg ? res.extra.header_bg : that.data.domain + "img/top.png",
                    level_type: res.extra.level_type,
                    m_apply: res.extra.m_apply,
                    leader_ing: res.extra.leader_ing,
                    leader_apply_display: res.extra.leader_apply_display ? res.extra.leader_apply_display : 1,
                    new_member_audit: res.extra.new_member_audit,
                    //会员免审 0 免审 1审核
                    userAuditStatus: res.extra.userAuditStatus ? res.extra.userAuditStatus : 2,
                    //会员审核状态，1待审核，2正常使用，3需申请、重新申请
                    custom_title: res.extra.level_set.custom_title,
                    leader_entrance_display: res.extra.leader_entrance_display,
                    leader_info_display: res.extra.leader_info_display,
                    service_qr_switch: res.extra.service_qr_switch,
                    service_qr: res.extra.service_qr,
                    userAuditStatus: res.extra.userAuditStatus,
                    t_cart_num: res.extra.cart_total_num,
                    need_wxaccount_auth: res.extra.need_wxaccount_auth
                });
                //处理模块开关
                                var features = common.getModulesSwich(res.extra.features);
                //处理数据格式
                                that.setData({
                    features: features
                });
            }
        });
    },
    goToNewPage: function(e) {
        var item = encodeURIComponent(JSON.stringify(e.currentTarget.dataset.item));
        wx.navigateTo({
            url: "/pages/newcomers/newcomers?item=" + item
        });
    },
    getMyLeaderInfo: function(leader_id_share) {
        common.request({
            endpoint: "leader/myleaderinfo",
            method: "post",
            data: {
                leader_id_share: leader_id_share,
                openid: wx.getStorageSync("user").openid
            },
            success: res => {
                this.setData({
                    myleaderinfo: res.extra
                });
            }
        });
    },
    showQrService: function() {
        let service_qr = [ this.data.service_qr ];
        wx.previewImage({
            urls: service_qr
        });
    },
    getData: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    info: res.extra,
                    platStatus: res.extra.uni_mem
                });
                if (res.extra.nickname == "" || res.extra.nickname == "微信用户") {
                    that.setData({
                        user_info_notice: 0
                    });
                }
            }
        });
    },
    getAmount: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/extract",
            method: "post",
            data: {
                openid: openid,
                day: 0
            },
            success: res => {
                that.setData({
                    amount0: res.extra.recommend_brokerage
                });
            }
        });
        common.request({
            endpoint: "My/myinfo/extract",
            method: "post",
            data: {
                openid: openid,
                day: 1
            },
            success: res => {
                that.setData({
                    amount1: res.extra.recommend_brokerage
                });
            }
        });
        common.request({
            endpoint: "My/myinfo/extract",
            method: "post",
            data: {
                openid: openid,
                day: 7
            },
            success: res => {
                that.setData({
                    amount7: res.extra.recommend_brokerage
                });
            }
        });
        common.request({
            endpoint: "My/myinfo/extract",
            method: "post",
            data: {
                openid: openid,
                day: 30
            },
            success: res => {
                that.setData({
                    amount30: res.extra.recommend_brokerage
                });
            }
        });
    },
    //判断资质规则按钮显示隐藏
    //资质、规则都为空，隐藏
    //请求封装起来，别一个按钮一个请求，技术组长后面要安排全面调整 hgj 2019.11.20
    rulesIsShowOrHide() {
        var that = this;
        common.request({
            endpoint: "set/intelligence1",
            method: "post",
            success(aptitude) {
                common.request({
                    endpoint: "set/intelligence_list",
                    method: "post",
                    success(rules) {
                        if (!aptitude.extra.data.intelligence && rules.extra.data.length == 0) {
                            //资质为空并且规则列表数组长度等于0，隐藏
                            that.setData({
                                rulesIsHide: true
                            });
                        } else {
                            //否则显示
                            that.setData({
                                rulesIsHide: false
                            });
                        }
                    }
                });
            }
        });
    },
    alert: function() {
        var alert = this.data.alert;
        if (alert == 0) {
            alert = 1;
        } else {
            alert = 0;
        }
        this.setData({
            alert: alert
        });
    },
    makeposter: function() {
        wx.navigateTo({
            url: "/pages/share2/share2?types=group"
        });
    },
    //请求基本信息
    getuserinfo: function() {
        common.request({
            endpoint: "/My/myinfo/is_edit",
            method: "post",
            data: {
                openid: wx.getStorageSync("user").openid
            },
            success: res => {
                this.setData({
                    close: 0
                });
            }
        });
    },
    getUserProfileApplyGroup() {
        let that = this;
        var leaderName = that.data.leaderName;
        var openid = wx.getStorageSync("user").openid;
        common.getUserInfoNew({
            success(res) {
                if (that.data.info.flag.flag == "5") {
                    wx.showModal({
                        title: that.data.alert_title,
                        content: leaderName + that.data.Theapplicationis
                    });
                } else {
                    wx.navigateTo({
                        url: "/pages/group_info/group_info"
                    });
                }
            }
        });
    },
    groupEntrance: function() {
        var that = this;
        var openid = "";
        var clerk = that.data.clerk;
        var leaderName = that.data.leaderName;
        if (clerk.wom_status == 0) {
            openid = wx.getStorageSync("user").openid;
        } else {
            openid = clerk.leader_openid;
        }
        common.getUserInfoNew({
            success(res) {
                common.request({
                    endpoint: "Leader/checkstatus/leaderentry",
                    method: "post",
                    data: {
                        openid: openid
                    },
                    success: res => {
                        if (res.extra.code_n == 1) {
                            wx.reLaunch({
                                url: "/leaderPerson/pages/center/center"
                            });
                        } else if (res.extra.code_n == 0) {
                            wx.showModal({
                                title: that.data.alert_title,
                                content: leaderName + that.data.Theaccounthasbeenfrozen
                            });
                        }
                    }
                });
            }
        });
    },
    getShareGroup: function(leader_id) {
        var that = this;
        common.request({
            endpoint: "leader/leader_get",
            method: "post",
            data: {
                leader_id: leader_id
            },
            success: res => {
                that.setData({
                    group: res.extra
                });
            }
        });
    },
    getGroup: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "leader/leader_index",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    group: res.extra
                });
            },
            fail: res => {
                if (res.code == 2) {}
            }
        });
    },
    leader_change: function(e) {
        let that = this;
        var address = e.currentTarget.dataset.address;
        var leaderName = that.data.leaderName;
        var is_leader = wx.getStorageSync("my_leader").is_leader;
        if (is_leader == 1) {
            wx.showModal({
                title: that.data.alert_title,
                content: that.data.YouAre + leaderName + that.data.Cantswitch + leaderName + "！"
            });
        }
        if (this.data.leader_change == 1 && is_leader != 1) {
            wx.navigateTo({
                url: "/pages/nearby_group2/nearby_group2"
            });
        }
    },
    //核销员
    clerk: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/writeoffman/query",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    clerk: res.extra
                });
                let clerk = {
                    wom_status: res.extra.wom_status,
                    leader_openid: res.extra.leader_openid
                };
                wx.setStorage({
                    key: "clerk",
                    data: clerk
                });
            }
        });
    },
    callService: function(e) {
        var phone = e.currentTarget.dataset.phone;
        if (phone) {
            wx.makePhoneCall({
                phoneNumber: phone
            });
        }
    },
    showCode: function() {
        var that = this;
        let openid = wx.getStorageSync("user").openid;
        common.getUserInfoNew({
            success(res) {
                common.request({
                    endpoint: "My/qrcode/checkqrcode",
                    method: "post",
                    data: {
                        page: "pages/pick_good_detail/pick_good_detail",
                        openid: openid
                    },
                    success: res => {
                        that.setData({
                            showCode: 1,
                            code_img: res.extra
                        });
                    }
                });
            }
        });
    },
    closeCode: function() {
        this.setData({
            showCode: 0
        });
    },
    getOrdernum: function() {
        var that = this;
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "Order/orderinfo/order_count",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    orderNum: res.extra
                });
            }
        });
    },
    getVersion: function() {
        let that = this;
        common.request({
            endpoint: "set/copyright",
            method: "post",
            data: {},
            success: res => {
                var copyText = [];
                if (res.extra.copyright_title) {
                    var copy = res.extra.copyright_title;
                    if (copy.indexOf(",") != -1) {
                        copyText = copy.split(",");
                    } else {
                        copyText[0] = copy;
                    }
                }
                that.setData({
                    version: res.extra,
                    copyTitle: copyText
                });
            }
        });
    },
    clickVersion: function() {
        let that = this;
        let version = that.data.version;
        if (version.open_mode == 0) {
            wx.makePhoneCall({
                phoneNumber: version.copyright_phone
            });
        } else {
            wx.navigateTo({
                url: "/pages/version/version"
            });
        }
    },
    //余额功能是否开通
    balanceIsOpen() {
        var that = this;
        common.request({
            endpoint: "Balance/balance/balance_switch",
            method: "post",
            data: {},
            success(res) {
                that.setData({
                    balanceSwitch: res.extra
                });
            }
        });
        common.request({
            endpoint: "Balance/balance/balance-set-read",
            method: "post",
            data: {},
            success(res) {
                that.setData({
                    balanceCashSwitch: res.extra.switch
                });
            }
        });
    },
    //跳转余额页面
    goToBalance() {
        common.getUserInfoNew({
            success() {
                wx.navigateTo({
                    url: "/pages/prePaid/prePaid"
                });
            }
        });
    },
    goSpecDetail: function() {
        common.getUserInfoNew({
            success() {
                wx.navigateTo({
                    url: "/pages/specDetail/specDetail"
                });
            }
        });
    },
    getTotalData() {
        var that = this;
        common.request({
            endpoint: "My/myinfo/my_tab_info",
            method: "post",
            success(res) {
                let totalData = res.extra;
                totalData.balance = util.numberFormatter(totalData.balance, 2);
                totalData.point = util.numberFormatter(totalData.point, 2);
                totalData.spec_fund = util.numberFormatter(totalData.spec_fund, 2);
                that.setData({
                    totalData: totalData
                });
            }
        });
    },
    getUserProfile() {
        common.getUserInfoNew();
    },
    authorLogin() {
        var that = this;
        common.getUserInfoNew({});
    },
    loginSuccess(e) {
        var user = e.detail.user;
        var info = this.data.info;
        info.img = user.avatar;
        info.nickname = user.nickname;
        this.setData({
            info: info
        });
    },
    goToOtherFunction(e) {
        common.getUserInfoNew({
            url: e.currentTarget.dataset.url
        });
    },
    goToLeaderFunction(e) {
        common.getUserInfoNew({
            url: e.currentTarget.dataset.url
        });
    },
    applyLevel: function(e) {
        let that = this;
        let openid = wx.getStorageSync("user").openid;
        common.getUserInfoNew({
            success() {
                if (that.data.info.level_condition == 1) {
                    //无条件申请
                    wx.showModal({
                        title: that.data.alert_title,
                        content: "是否申请" + that.data.custom_title,
                        success(res) {
                            if (res.confirm) {
                                that.apply();
                            }
                        }
                    });
                } else {
                    wx.navigateTo({
                        url: e.currentTarget.dataset.url
                    });
                }
            }
        });
    },
    applyMedical: function() {
        if (this.data.shop_status == 1) {
            wx.showModal({
                title: that.data.alert_title,
                content: that.data.Haveappliedforfreeshop,
                showCancel: false
            });
            return false;
        }
        common.getUserInfoNew({
            success() {
                wx.navigateTo({
                    url: "/pages/medical/medical"
                });
            }
        });
    },
    apply: function() {
        let that = this;
        common.request({
            endpoint: "/Level/level/apply",
            method: "post",
            data: {},
            success: res => {
                wx.showToast({
                    title: that.data.Applicationsubmitted,
                    icon: "none"
                });
                setTimeout(() => {
                    wx.reLaunch({
                        url: "/pages/personal/personal"
                    });
                }, 1500);
            },
            fail(err) {
                wx.showModal({
                    title: that.data.alert_title,
                    content: err.message,
                    showCancel: false
                });
            }
        });
    },
    edit(e) {
        common.getUserInfoNew({
            url: e.currentTarget.dataset.url
        });
    },
    /**
   * 改变购物车上标数字
   */
    changeIconNum(num, index) {
        let that = this;
        var tabBar = that.data.tabBar;
        tabBar[index].num = num;
        that.setData({
            tabBar: tabBar
        });
    },
    /**
   * 判断是否为本地商家
   */
    getMerStatus: function() {
        var that = this;
        that.setData({
            menu1: unescape(that.data.menu1),
            menu2: unescape(that.data.menu2)
        });
        common.request({
            endpoint: "Localbusiness/merchant/merchant-detail",
            method: "post",
            data: {
                user_id: that.data.uid
            },
            success: res => {
                that.setData({
                    MerStatus: res.extra ? res.extra.status : "0"
                });
            }
        });
    },
    goToApplyLocal: function(e) {
        let that = this;
        let openid = wx.getStorageSync("user").openid;
        common.getUserInfoNew({
            success() {
                common.request({
                    endpoint: "Localbusiness/merchant/merchant-detail",
                    method: "post",
                    data: {
                        user_id: that.data.uid
                    },
                    success: res => {
                        let res_status = res.extra ? res.extra.status : "0";
                        if (res_status == "2") {
                            let id = res.extra.id;
                            wx.navigateTo({
                                url: "/localLifePackage/pages/codeApply1/codeApply1?id=" + id
                            });
                        } else {
                            wx.navigateTo({
                                url: "/localLifePackage/pages/appLocal/appLocal?status=" + res_status
                            });
                        }
                    }
                });
            }
        });
    },
    goTovipPage: function() {
        let that = this;
        let openid = wx.getStorageSync("user").openid;
        if (that.data.vipStatus == 0) {
            common.getUserInfoNew({
                success() {
                    wx.navigateTo({
                        url: "/memberVip/pages/memberAuthor/memberAuthor?vipStatus=" + that.data.vipStatus
                    });
                }
            });
        } else if (that.data.vipStatus == 1 || that.data.vipStatus == 2 || that.data.vipStatus == 3) {
            wx.navigateTo({
                url: "/memberVip/pages/memberOpen/memberOpen?vipStatus=" + that.data.vipStatus
            });
        } else if (that.data.vipStatus == -1) {
            common.getUserInfoNew({
                success() {
                    wx.navigateTo({
                        url: "/memberVip/pages/memberOpen/memberOpen?vipStatus=" + that.data.vipStatus
                    });
                }
            });
        }
    },
    goBrandManage: function() {
        let that = this;
        let openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    info: res.extra
                });
                if (that.data.info.merchant_status == 2) {
                    wx.navigateTo({
                        url: "/brandManage/pages/brandIndex/brandIndex?name=" + that.data.info.merchant_name
                    });
                } else if (that.data.info.merchant_status == 1) {
                    wx.showModal({
                        title: that.data.alert_title,
                        content: that.data.Brandtenantsareunder,
                        showCancel: false
                    });
                } else if (that.data.info.merchant_status == 3) {
                    wx.showModal({
                        title: that.data.alert_title,
                        content: that.data.Brandtenanthasexpired,
                        showCancel: false
                    });
                } else if (that.data.info.merchant_status == 4) {
                    wx.navigateTo({
                        url: "/brandManage/pages/applyBrand_manage/applyBrand_manage"
                    });
                }
            }
        });
    },
    getPhoneNumber(e) {
        let that = this;
        var errMsg = e.detail.errMsg;
        var iv = e.detail.iv;
        var encryptedData = e.detail.encryptedData;
        common.getUserInfoNew({
            success() {
                common.request({
                    endpoint: "My/myinfo/bind-phone",
                    method: "post",
                    data: {
                        iv: iv,
                        encryptedData: encryptedData,
                        jscode: that.data.jscode
                    },
                    success: res => {
                        wx.showToast({
                            title: that.data.Bindthephonesuccessfully
                        });
                    },
                    fail(err) {
                        wx.showModal({
                            title: that.data.alert_title,
                            content: that.data.Failedtobindphone
                        });
                    }
                });
            }
        });
    },
    login() {
        let that = this;
        wx.login({
            success(res) {
                that.setData({
                    jscode: res.code
                });
            }
        });
    },
    goToDistributor: function(e) {
        let that = this;
        let openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    info: res.extra
                });
                if (that.data.info.level_status == 0) {
                    wx.showModal({
                        title: that.data.alert_title,
                        content: that.data.custom_title + "正在申请中!",
                        showCancel: false
                    });
                    return false;
                } else if (that.data.info.level_status == 2) {
                    wx.showModal({
                        title: that.data.alert_title,
                        content: that.data.custom_title + "已冻结",
                        showCancel: false
                    });
                    return false;
                } else if (that.data.info.level_status == 1) {
                    common.getUserInfoNew({
                        success() {
                            wx.navigateTo({
                                url: "/distributor/pages/applyPerson/applyPerson"
                            });
                        }
                    });
                } else if (that.data.info.level_status == -1) {
                    common.getUserInfoNew({
                        success() {
                            if (that.data.info.level_condition == 1) {
                                //无条件申请
                                wx.showModal({
                                    title: that.data.alert_title,
                                    content: "是否申请" + that.data.custom_title,
                                    success(res) {
                                        if (res.confirm) {
                                            that.apply();
                                        }
                                    }
                                });
                            } else {
                                wx.navigateTo({
                                    url: "/distributor/pages/applyDistributor/applyDistributor"
                                });
                            }
                        }
                    });
                }
            }
        });
    },
    user_info_notice_close: function() {
        this.setData({
            user_info_notice: 1
        });
    },
    relTap: function(e) {
        let that = this;
        let openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "My/myinfo/myinfo",
            method: "post",
            data: {
                openid: openid
            },
            success: res => {
                that.setData({
                    info: res.extra
                });
                if (that.data.info.rider == 1) {
                    //可以申请
                    wx.navigateTo({
                        url: "/riderDistribution/pages/register/register"
                    });
                } else if (that.data.info.rider == 3) {
                    //申请中
                    wx.showToast({
                        icon: "none",
                        title: "骑手申请正在审核中"
                    });
                } else if (that.data.info.rider == 2) {
                    //是骑手
                    wx.navigateTo({
                        url: "/riderDistribution/pages/order/order"
                    });
                }
            }
        });
    },
    //选择头像
    onChooseAvatar(e) {
        let that = this;
        let domian = app.globalData.domain;
        var uniacid = wx.getStorageSync("uniacid");
        let avatar_img = e.detail.avatarUrl;
        wx.uploadFile({
            url: domian + "Lodger/lodger/product-img-upload",
            filePath: avatar_img,
            name: "file",
            formData: {},
            header: {
                "Content-Type": "multipart/form-data",
                uniacid: uniacid,
                listint: 1,
                type: 99
            },
            success: function(res) {
                var avatar_url = JSON.parse(res.data);
                that.setData({
                    ["info.img"]: avatar_url.extra.url
                });
                that.formSubmit(1);
            },
            fail: function(res) {
                wx.showModal({
                    title: "错误提示",
                    content: "上传图片失败，请重试",
                    showCancel: false,
                    success: function(res) {}
                });
            }
        });
    },
    changeNickname(e) {
        this.setData({
            ["info.nickname"]: e.detail.value
        });
        this.formSubmit(1);
    },
    formSubmit: function(vvv) {
        let that = this;
        let type = vvv;
        let e = {
            detail: {
                value: {
                    img: that.data.info.img,
                    nickname: that.data.info.nickname
                }
            }
        };
        var openid = wx.getStorageSync("user").openid;
        common.request({
            endpoint: "member/saveinfo",
            method: "post",
            data: {
                openid: openid,
                info: e.detail.value
            },
            header: {
                "content-type": "application/json"
            },
            success: res => {
                wx.showToast({
                    title: "保存成功",
                    icon: "success",
                    success: res => {}
                });
            },
            fail: res => {
                wx.showToast({
                    title: "保存失败",
                    icon: "none"
                });
            }
        });
    }
});